// pages/feedback/index.js
const domain = require('../../config').domain;
const feedback_list_url = require('../../config').feedback_list_url;
const feedback_add_url = require('../../config').feedback_add_url;
var category_id = '';
var app = getApp()
var page_size = 10;
var page = 1;
var is_get = true;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    domain: domain,
    settings: {},
    head_data: {},
    category: {},
    feedback_content: '',
  },
  left_nav_btn: function (event) {
    this.setData({
      'head_data.left_nav_show': "show"
    })
  },
  left_nav_mask_btn: function (event) {
    this.setData({
      'head_data.left_nav_show': "hidden"
    })
  },
  formSubmit: function (e) {
    var that = this
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    if (e.detail.value.content == null || e.detail.value.content == ""){
      wx.hideLoading()
      wx.showModal({
        title: '提示',
        content: '内容不能为空！',
        success: function (res) {
        }
      })
      return false;
    }
    wx.request({
      url: feedback_add_url, //仅为示例，并非真实的接口地址
      data: {
        content: e.detail.value.content,
        member_id: -1
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        //console.log(res)
        if (res.data.code == 200) {
          that.setData({
            feedback_content: ''
          });
          wx.showToast({
            title: res.data.msg,
            icon: 'success',
            duration: 2000
          })
          wx.redirectTo({
            url: '/pages/feedback/index'
          })
        } else {
          wx.hideLoading()
          wx.showModal({
            title: '提示',
            content: res.data.msg,
            success: function (res) {
            }
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    category_id = options.category_id;
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    var that = this
    wx.getStorage({
      key: 'settings',
      success: function (res) {
        that.setData({
          settings: res.data,
        });
      }
    })
    wx.getStorage({
      key: 'categorys',
      success: function (res) {
        that.setData({
          'head_data.categorys': res.data,
          category: res.data[category_id]
        });
      }
    })
    //留言
    wx.request({
      url: feedback_list_url,
      data: {
        order: 'id desc',
        page_size: page_size,
        page: page
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        that.setData({
          feedbacks: res.data.data
        });
        if (res.data.current_page >= Math.ceil(res.data.total / res.data.per_page)) {
          is_get = false
        }
      }
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    wx.hideLoading()
    wx.showShareMenu({
      withShareTicket: true
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this
    if (is_get) {
      wx.showLoading({
        title: '加载中',
        mask: true
      })
      page++;
      wx.request({
        url: feedback_list_url,
        data: {
          order: 'id desc',
          page_size: page_size,
          page: page
        },
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          var feedbacks = that.data.feedbacks;
          feedbacks = feedbacks.concat(res.data.data)
          that.setData({
            feedbacks: feedbacks
          });
          wx.hideLoading()
          if (res.data.current_page >= Math.ceil(res.data.total / res.data.per_page)) {
            is_get = false
          }
        }
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})